<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body,
      html {
        position: relative;
        overflow: hidden;
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
      }
      #container {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 200px;
        height: 200px;
        transform: translate(-50%, -50%);
        overflow: hidden;
      }
      .bg {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        transition: all 0.5s ease;
      }
      .item {
        width: 100%;
        height: 100%;
        background-size: cover;
      }
      .item:nth-child(1) {
        position: relative;
        background-image: url("./1.jpg");
      }
      .item:nth-child(2) {
        background-image: url("./2.jpg");
      }
      .item:nth-child(3) {
        background-image: url("./3.jpg");
        transform: translate(0px, -300%);
      }
      .item:nth-child(4) {
        background-image: url("./4.jpg");
        transform: translate(100%, -300%);
      }
      .item:nth-child(5) {
        background-image: url("./5.jpg");
        transform: translate(-100%, -400%);
      }
      .right{
        transform: translate(-100%,0);
      }
      .left{
        transform: translate(100%,0);
      }
      .up{
        transform: translate(0,100%);
      }
      .down{
        transform: translate(0,-100%);
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div class="bg">
        <div class="item center"></div>
        <div class="item img-bottom"></div>
        <div class="item img-top">
          <a href="https://www.baidu.com">111</a>
        </div>
        <div class="item img-right"></div>
        <div class="item img-left"></div>
      </div>
    </div>
    <script>
      const container = document.querySelector("#container");
      const bg = document.querySelector(".bg");

      const rect = container.getBoundingClientRect();
      // 再这里就是45度角对应的弧度
      const theta = Math.atan(rect.height / rect.width);

      container.addEventListener("mouseenter", (e) => {
        // 以中间为坐标原点
        const x = e.offsetX - rect.width / 2;
        const y = rect.height / 2 - e.offsetY;
        // console.log(e.offsetX,e.offsetY)
        // console.log(x,y)
        const d = Math.atan2(y, x);
        let dire;
        if (d < theta && d >= -theta) {
          dire = "right";
        } else if (d >= theta && d < Math.PI - theta) {
          dire = "up";
        } else if (d < -theta && d >= theta - Math.PI) {
          dire = "down";
        } else {
          dire = "left";
        }
        bg.classList.add(dire);
        console.log(dire);
      });
      container.addEventListener("mouseleave", () => {
        bg.className = "bg";
      });
    </script>
  </body>
</html>
